package com.acme.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.acme.dao.ClaimDAO;
import com.acme.dao.UserDAO;
import com.acme.dao.security.DBUserDAO;
import com.acme.entity.User;
import com.acme.entity.security.DBUserDetails;

@Transactional
@Controller
@RequestMapping(value = "MyAccount")
public class UserController {

	@Autowired
	private UserDAO userDao;

	@Autowired
	private ClaimDAO claimDAO;
	
	@Autowired
	private DBUserDAO dbUserDAO;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView user(HttpServletRequest request){

		ModelAndView mav = new ModelAndView();

		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		User user = dbUserDAO.getUserByUsername(username);
		
		mav = new ModelAndView("MyAccount", "myClaims", claimDAO.getClaimsByUser(user));

		return mav;
	}
	
	@RequestMapping(value="/EditUser", method = RequestMethod.POST) 
	public ModelAndView editInfo(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		User user = dbUserDAO.getUserByUsername(username);
		
		mav = new ModelAndView("EditUser", "curUser", user);
		
		return mav;
	}
}